LiquidBlock = class(Block)

function LiquidBlock:init(id,tex)
    Block.init(self,id,tex,0)
    self.solidBlock = false
    self.opacityBlock = true
    self.liquidBlock = true
end

function LiquidBlock:shouldRenderFace(x,y,z,face)
    if not world:isBlockLoaded(x,z) then return false end
    local block = Block.blocks[world:getBlock(x,y,z)]
    local lb = block.opacityBlock
    if face == Top and self:shouldRenderFaceTop(x,y,z) then
        lb = true
    end
    return lb and (not block.liquidBlock)
end

function LiquidBlock:shouldRenderFaceC(c,x,y,z,face)
    local block = Block.blocks[c:getBlock(x,y,z)]
    local lb = block.opacityBlock
    if face == Top and self:shouldRenderFaceTopC(c,x,y,z) then
        lb = true
    end
    return lb and (not block.liquidBlock)
end

function LiquidBlock:shouldRenderFaceTop(x,y,z)
    return (world:isBlockLoaded(x - 1,z) and world:getBlock(x - 1,y,z) == 0) or (world:isBlockLoaded(x + 1,z) and world:getBlock(x + 1,y,z) == 0) or (world:isBlockLoaded(x,z - 1) and world:getBlock(x,y,z - 1) == 0) or (world:isBlockLoaded(x,z + 1) and world:getBlock(x,y,z + 1) == 0)
end

function LiquidBlock:shouldRenderFaceTopC(c,x,y,z)
    return c:getBlock(x - 1,y,z) == 0 or c:getBlock(x + 1,y,z) == 0 or c:getBlock(x,y,z - 1) == 0 or c:getBlock(x,y,z + 1) == 0
end

function LiquidBlock:getTesselator(ts)
    return ts[2]
end

function LiquidBlock:renderFace(t,x,y,z,face)
    local tex = self:getTex(face)
    local u0 = (tex % 16) / 16
    local u1 = u0 + 15.99 / 256
    local v0 = tex // 16 / 16
    local v1 = v0 + 15.99 / 256
    
    local x0 = x
    local x1 = x + 1
    local y0 = y - 0.1
    local y1 = y + 0.9
    local z0 = z
    local z1 = z + 1
    
    local c1 = 220
    local c2 = 200
    local c3 = 190
    local c4 = 155
    
    t:alphab(200)
    
    local b
    local bf = 1 - GameSetting.SmoothLight
    
    local b1,b2
    
    if face == Front then
        b = self:getBrightness(x,y,z + 1) * bf
        b1 = self:getBrightnessAtCoord(x,y,z + 1) + b
        b2 = self:getBrightnessAtCoord(x + 1,y + 1,z + 1) + b
        t:vertexUVCA(x0,y0,z1,u0,v0,c2 * b1)
        t:vertexUVCA(x1,y0,z1,u1,v0,c2 * (self:getBrightnessAtCoord(x + 1,y,z + 1) + b))
        t:vertexUVCA(x1,y1,z1,u1,v1,c2 * b2)
        t:vertexUVCA(x1,y1,z1,u1,v1,c2 * b2)
        t:vertexUVCA(x0,y1,z1,u0,v1,c2 * (self:getBrightnessAtCoord(x,y + 1,z + 1) + b))
        t:vertexUVCA(x0,y0,z1,u0,v0,c2 * b1)
    elseif face == Back then
        b = self:getBrightness(x,y,z - 1) * bf
        b1 = self:getBrightnessAtCoord(x + 1,y,z) + b
        b2 = self:getBrightnessAtCoord(x,y + 1,z) + b
        t:vertexUVCA(x1,y0,z0,u0,v0,c2 * b1)
        t:vertexUVCA(x0,y0,z0,u1,v0,c2 * (self:getBrightnessAtCoord(x,y,z) + b))
        t:vertexUVCA(x0,y1,z0,u1,v1,c2 * b2)
        t:vertexUVCA(x0,y1,z0,u1,v1,c2 * b2)
        t:vertexUVCA(x1,y1,z0,u0,v1,c2 * (self:getBrightnessAtCoord(x + 1,y + 1,z) + b))
        t:vertexUVCA(x1,y0,z0,u0,v0,c2 * b1)
    elseif face == Top then
        b = self:getBrightness(x,y + 1,z) * bf
        b1 = self:getBrightnessAtCoord(x,y + 1,z + 1) + b
        b2 = self:getBrightnessAtCoord(x + 1,y + 1,z) + b
        t:vertexUVCA(x0,y1,z1,u0,v0,c1 * b1)
        t:vertexUVCA(x1,y1,z1,u1,v0,c1 * (self:getBrightnessAtCoord(x + 1,y + 1,z + 1) + b))
        t:vertexUVCA(x1,y1,z0,u1,v1,c1 * b2)
        t:vertexUVCA(x1,y1,z0,u1,v1,c1 * b2)
        t:vertexUVCA(x0,y1,z0,u0,v1,c1 * (self:getBrightnessAtCoord(x,y + 1,z) + b))
        t:vertexUVCA(x0,y1,z1,u0,v0,c1 * b1)
    elseif face == Bottom then
        b = self:getBrightness(x,y - 1,z) * bf
        b1 = self:getBrightnessAtCoord(x,y,z) + b
        b2 = self:getBrightnessAtCoord(x + 1,y,z + 1) + b
        t:vertexUVCA(x0,y0,z0,u0,v0,c4 * b1)
        t:vertexUVCA(x1,y0,z0,u1,v0,c4 * (self:getBrightnessAtCoord(x + 1,y,z) + b))
        t:vertexUVCA(x1,y0,z1,u1,v1,c4 * b2)
        t:vertexUVCA(x1,y0,z1,u1,v1,c4 * b2)
        t:vertexUVCA(x0,y0,z1,u0,v1,c4 * (self:getBrightnessAtCoord(x,y,z + 1) + b))
        t:vertexUVCA(x0,y0,z0,u0,v0,c4 * b1)
    elseif face == Left then
        b = self:getBrightness(x - 1,y,z) * bf
        b1 = self:getBrightnessAtCoord(x,y,z) + b
        b2 = self:getBrightnessAtCoord(x,y + 1,z + 1) + b
        t:vertexUVCA(x0,y0,z0,u0,v0,c3 * b1)
        t:vertexUVCA(x0,y0,z1,u1,v0,c3 * (self:getBrightnessAtCoord(x,y,z + 1) + b))
        t:vertexUVCA(x0,y1,z1,u1,v1,c3 * b2)
        t:vertexUVCA(x0,y1,z1,u1,v1,c3 * b2)
        t:vertexUVCA(x0,y1,z0,u0,v1,c3 * (self:getBrightnessAtCoord(x,y + 1,z) + b))
        t:vertexUVCA(x0,y0,z0,u0,v0,c3 * b1)
    else
        b = self:getBrightness(x + 1,y,z) * bf
        b1 = self:getBrightnessAtCoord(x + 1,y,z + 1) + b
        b2 = self:getBrightnessAtCoord(x + 1,y + 1,z) + b
        t:vertexUVCA(x1,y0,z1,u0,v0,c3 * b1)
        t:vertexUVCA(x1,y0,z0,u1,v0,c3 * (self:getBrightnessAtCoord(x + 1,y,z) + b))
        t:vertexUVCA(x1,y1,z0,u1,v1,c3 * b2)
        t:vertexUVCA(x1,y1,z0,u1,v1,c3 * b2)
        t:vertexUVCA(x1,y1,z1,u0,v1,c3 * (self:getBrightnessAtCoord(x + 1,y + 1,z + 1) + b))
        t:vertexUVCA(x1,y0,z1,u0,v0,c3 * b1)
    end
end

function LiquidBlock:renderFaceC(t,c,x,y,z,face)
    local tex = self:getTex(face)
    local u0 = (tex % 16) / 16
    local u1 = u0 + 15.99 / 256
    local v0 = tex // 16 / 16
    local v1 = v0 + 15.99 / 256
    
    local xa,za = c.xPos + x,c.zPos + z
    local x0 = xa
    local x1 = xa + 1
    local y0 = y - 0.1
    local y1 = y + 0.9
    local z0 = za
    local z1 = za + 1
    
    local c1 = 220
    local c2 = 200
    local c3 = 190
    local c4 = 155
    
    t:alphab(200)
    
    local b
    local bf = 1 - GameSetting.SmoothLight
    
    local b1,b2
    
    if face == Front then
        b = self:getBrightnessC(c,x,y,z + 1) * bf
        b1 = self:getBrightnessAtCoordC(c,x,y,z + 1) + b
        b2 = self:getBrightnessAtCoordC(c,x + 1,y + 1,z + 1) + b
        t:vertexUVCA(x0,y0,z1,u0,v0,c2 * b1)
        t:vertexUVCA(x1,y0,z1,u1,v0,c2 * (self:getBrightnessAtCoordC(c,x + 1,y,z + 1) + b))
        t:vertexUVCA(x1,y1,z1,u1,v1,c2 * b2)
        t:vertexUVCA(x1,y1,z1,u1,v1,c2 * b2)
        t:vertexUVCA(x0,y1,z1,u0,v1,c2 * (self:getBrightnessAtCoordC(c,x,y + 1,z + 1) + b))
        t:vertexUVCA(x0,y0,z1,u0,v0,c2 * b1)
    elseif face == Back then
        b = self:getBrightnessC(c,x,y,z - 1) * bf
        b1 = self:getBrightnessAtCoordC(c,x + 1,y,z) + b
        b2 = self:getBrightnessAtCoordC(c,x,y + 1,z) + b
        t:vertexUVCA(x1,y0,z0,u0,v0,c2 * b1)
        t:vertexUVCA(x0,y0,z0,u1,v0,c2 * (self:getBrightnessAtCoordC(c,x,y,z) + b))
        t:vertexUVCA(x0,y1,z0,u1,v1,c2 * b2)
        t:vertexUVCA(x0,y1,z0,u1,v1,c2 * b2)
        t:vertexUVCA(x1,y1,z0,u0,v1,c2 * (self:getBrightnessAtCoordC(c,x + 1,y + 1,z) + b))
        t:vertexUVCA(x1,y0,z0,u0,v0,c2 * b1)
    elseif face == Top then
        b = self:getBrightnessC(c,x,y + 1,z) * bf
        b1 = self:getBrightnessAtCoordC(c,x,y + 1,z + 1) + b
        b2 = self:getBrightnessAtCoordC(c,x + 1,y + 1,z) + b
        t:vertexUVCA(x0,y1,z1,u0,v0,c1 * b1)
        t:vertexUVCA(x1,y1,z1,u1,v0,c1 * (self:getBrightnessAtCoordC(c,x + 1,y + 1,z + 1) + b))
        t:vertexUVCA(x1,y1,z0,u1,v1,c1 * b2)
        t:vertexUVCA(x1,y1,z0,u1,v1,c1 * b2)
        t:vertexUVCA(x0,y1,z0,u0,v1,c1 * (self:getBrightnessAtCoordC(c,x,y + 1,z) + b))
        t:vertexUVCA(x0,y1,z1,u0,v0,c1 * b1)
    elseif face == Bottom then
        b = self:getBrightnessC(c,x,y - 1,z) * bf
        b1 = self:getBrightnessAtCoordC(c,x,y,z) + b
        b2 = self:getBrightnessAtCoordC(c,x + 1,y,z + 1) + b
        t:vertexUVCA(x0,y0,z0,u0,v0,c4 * b1)
        t:vertexUVCA(x1,y0,z0,u1,v0,c4 * (self:getBrightnessAtCoordC(c,x + 1,y,z) + b))
        t:vertexUVCA(x1,y0,z1,u1,v1,c4 * b2)
        t:vertexUVCA(x1,y0,z1,u1,v1,c4 * b2)
        t:vertexUVCA(x0,y0,z1,u0,v1,c4 * (self:getBrightnessAtCoordC(c,x,y,z + 1) + b))
        t:vertexUVCA(x0,y0,z0,u0,v0,c4 * b1)
    elseif face == Left then
        b = self:getBrightnessC(c,x - 1,y,z) * bf
        b1 = self:getBrightnessAtCoordC(c,x,y,z) + b
        b2 = self:getBrightnessAtCoordC(c,x,y + 1,z + 1) + b
        t:vertexUVCA(x0,y0,z0,u0,v0,c3 * b1)
        t:vertexUVCA(x0,y0,z1,u1,v0,c3 * (self:getBrightnessAtCoordC(c,x,y,z + 1) + b))
        t:vertexUVCA(x0,y1,z1,u1,v1,c3 * b2)
        t:vertexUVCA(x0,y1,z1,u1,v1,c3 * b2)
        t:vertexUVCA(x0,y1,z0,u0,v1,c3 * (self:getBrightnessAtCoordC(c,x,y + 1,z) + b))
        t:vertexUVCA(x0,y0,z0,u0,v0,c3 * b1)
    else
        b = self:getBrightnessC(c,x + 1,y,z) * bf
        b1 = self:getBrightnessAtCoordC(c,x + 1,y,z + 1) + b
        b2 = self:getBrightnessAtCoordC(c,x + 1,y + 1,z) + b
        t:vertexUVCA(x1,y0,z1,u0,v0,c3 * b1)
        t:vertexUVCA(x1,y0,z0,u1,v0,c3 * (self:getBrightnessAtCoordC(c,x + 1,y,z) + b))
        t:vertexUVCA(x1,y1,z0,u1,v1,c3 * b2)
        t:vertexUVCA(x1,y1,z0,u1,v1,c3 * b2)
        t:vertexUVCA(x1,y1,z1,u0,v1,c3 * (self:getBrightnessAtCoordC(c,x + 1,y + 1,z + 1) + b))
        t:vertexUVCA(x1,y0,z1,u0,v0,c3 * b1)
    end
end

function LiquidBlock:blockBeSet(x,y,z)
    self:addToEntityBlock(x,y,z)
end

function LiquidBlock:neighborChanged(x,y,z,face,block)
    self:addToEntityBlock(x,y,z)
end

function LiquidBlock:addToEntityBlock(x,y,z)
    world.entityBlockList:add(EntityLiquidBlock(self,x,y,z))
end

function LiquidBlock:getLayer()
    return 2
end